package relatorio;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;


public class relatLocacao {

    private static Connection getConnection() throws
            ClassNotFoundException, SQLException {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost/biblioteca";
        String user = "root";
        String password = "123";
        Class.forName(driver);
        Connection con = (Connection) DriverManager.getConnection(url, user, password);
        return con;

    }

    public void geraRelatorio() throws ClassNotFoundException, SQLException, JRException {
        Connection con = getConnection();
        Statement stm = (Statement) con.createStatement();
        String query = "select aluno.nome,serie,turma,id,livro.id_livro,titulo,num,locacao.data_saida,"
                + "data_devolucao from aluno,livro,locacao where "
                + "locacao.id_aluno=aluno.id and locacao.id_livro=livro.id_livro order by data_saida,serie,turma,nome";
        ResultSet rs = (ResultSet) stm.executeQuery(query);
        JRResultSetDataSource jrRS = new JRResultSetDataSource(rs);
        JasperPrint jasperPrint = JasperFillManager.fillReport("relatorio/emprestimo.jasper", new HashMap(), jrRS);
        JasperViewer.viewReport(jasperPrint, false);
    }
public static void main(String[]args) throws SQLException, ClassNotFoundException{
        try {
            new relatLocacao().geraRelatorio();
        } catch (JRException ex) {
            Logger.getLogger(relatLocacao.class.getName()).log(Level.SEVERE, null, ex);
        }
       
}
}
